import { message } from 'antd';
import { queryModelClassTree } from '@/services/modelclass';
import {
  queryProductModel,
  add,
  edit,
  del,
  qryById,
  queryProductModelProperty,
  qryUnitList,
  qryDataTypeList,
  addProperty,
  qryPropertyDetail,
  editProperty,
  delProperty,
} from '@/services/productmodel';

export default {
  namespace: 'productmodel',
  state: {
    list: [],
    treeData: [],
    pagination: { pageSize: 10 },
    current: {},
    detail: {},
    propertyList: [],
    unitList: [],
    dataTypeList: [],
    propertyDetail: {},
  },
  effects: {
    *fetch({ payload }, { call, put }) {
      const response = yield call(queryProductModel, payload);
      if (response.httpCode === 200) {
        yield put({
          type: 'save',
          payload: response,
        });
      } else {
        message.error({ content: response.msg, key: 1 });
      }
    },
    *fetchProperty({ payload }, { call, put }) {
      const response = yield call(queryProductModelProperty, payload);
      if (response.httpCode === 200) {
        yield put({
          type: 'saveProperty',
          payload: response,
        });
      } else {
        message.error({ content: response.msg, key: 1 });
      }
    },
    *tree({ payload }, { call, put }) {
      const response = yield call(queryModelClassTree, payload);
      if (response.httpCode === 200) {
        yield put({
          type: 'saveTree',
          payload: response,
        });
      } else {
        message.error({ content: response.msg, key: 1 });
      }
    },
    *add({ payload, form, handleModalVisible }, { call, put }) {
      const response = yield call(add, payload);
      if (response.httpCode === 200) {
        message.success(response.msg);
        form.resetFields();
        handleModalVisible();
        yield put({ type: 'fetch' });
      } else {
        message.error({ content: response.msg, key: 1 });
      }
    },
    *edit({ payload, form, handleModalVisible }, { call, put }) {
      const response = yield call(edit, payload);
      if (response.httpCode === 200) {
        message.success(response.msg);
        form.resetFields();
        handleModalVisible();
        yield put({
          type: 'fetchDetail',
          payload: payload.id,
        });
      } else {
        message.error({ content: response.msg, key: 1 });
      }
    },
    *del({ payload }, { call, put }) {
      const response = yield call(del, payload);
      if (response.httpCode === 200) {
        message.success(response.msg);
        yield put({ type: 'fetch' });
      } else {
        message.error({ content: response.msg, key: 1 });
      }
    },
    *fetchDetail({ payload }, { call, put }) {
      const response = yield call(qryById, payload);
      if (response.httpCode === 200) {
        yield put({
          type: 'saveDetail',
          payload: response,
        });
      } else {
        message.error({ content: response.msg, key: 1 });
      }
    },
    *fetchUnit({ payload }, { call, put }) {
      const response = yield call(qryUnitList, payload);
      if (response.httpCode === 200) {
        yield put({
          type: 'saveUnit',
          payload: response,
        });
      } else {
        message.error({ content: response.msg, key: 1 });
      }
    },
    *fetchDataType({ payload }, { call, put }) {
      const response = yield call(qryDataTypeList, payload);
      if (response.httpCode === 200) {
        yield put({
          type: 'saveDataType',
          payload: response,
        });
      } else {
        message.error({ content: response.msg, key: 1 });
      }
    },
    *addProperty({ payload, form, handleAddModalVisible }, { call, put }) {
      const response = yield call(addProperty, payload);
      if (response.httpCode === 200) {
        message.success(response.msg);
        form.resetFields();
        handleAddModalVisible();
        yield put({
          type: 'fetchProperty',
          payload: { modelId: payload.modelId },
        });
      } else {
        message.error({ content: response.msg, key: 1 });
      }
    },
    *editProperty({ payload, form, handleEditModalVisible }, { call, put }) {
      const response = yield call(editProperty, payload);
      if (response.httpCode === 200) {
        message.success(response.msg);
        form.resetFields();
        handleEditModalVisible();
        yield put({
          type: 'fetchProperty',
          payload: { modelId: payload.modelId },
        });
      } else {
        message.error({ content: response.msg, key: 1 });
      }
    },
    *fetchPropertyDetail({ payload, handleEditModalVisible }, { call, put }) {
      const response = yield call(qryPropertyDetail, payload);
      if (response.httpCode === 200) {
        handleEditModalVisible(true);
        yield put({
          type: 'savePropertyDetail',
          payload: response,
        });
      } else {
        message.error({ content: response.msg, key: 1 });
      }
    },
    *showView({ payload, handleViewModalVisible }, { call, put }) {
      const response = yield call(qryPropertyDetail, payload);
      if (response.httpCode === 200) {
        handleViewModalVisible(true);
        yield put({
          type: 'savePropertyDetail',
          payload: response,
        });
      } else {
        message.error({ content: response.msg, key: 1 });
      }
    },
    *delProperty({ payload, productModelId }, { call, put }) {
      const response = yield call(delProperty, payload);
      if (response.httpCode === 200) {
        message.success(response.msg);
        yield put({
          type: 'fetchProperty',
          payload: { modelId: productModelId },
        });
      } else {
        message.error({ content: response.msg, key: 1 });
      }
    },
  },
  reducers: {
    save(state, action) {
      return {
        ...state,
        list: action.payload.data,
        pagination: {
          current: action.payload.current,
          pageSize: action.payload.size,
          total: action.payload.iTotalRecords,
        },
      };
    },
    saveTree(state, action) {
      return {
        ...state,
        treeData: action.payload.data,
      };
    },
    saveDetail(state, action) {
      return {
        ...state,
        detail: action.payload.data,
      };
    },
    savePropertyDetail(state, action) {
      return {
        ...state,
        propertyDetail: action.payload.data,
      };
    },
    saveProperty(state, action) {
      return {
        ...state,
        propertyList: action.payload.data,
        pagination: {
          current: action.payload.current,
          pageSize: action.payload.size,
          total: action.payload.iTotalRecords,
        },
      };
    },
    saveUnit(state, action) {
      return {
        ...state,
        unitList: action.payload.data,
      };
    },
    saveDataType(state, action) {
      return {
        ...state,
        dataTypeList: action.payload.data,
      };
    },
  },
};
